#include "stdafx.h"
#include "ui_image.h"

_SSL_BEGIN
_SSUIL_BEGIN

UIImage::UIImage()
{
	//
	background_transparent_ = true;
	show_border_ = false;

	//

	// preset_image_
	bitmap_h_ = NULL;
}

UIImage::~UIImage()
{
	if(bitmap_h_ != NULL)
	{
		DeleteObject(bitmap_h_);
		bitmap_h_ = NULL;
	}
}

void UIImage::SetImage(const UIPresetImage &img)
{
	preset_image_ = img;
	if(bitmap_h_ != NULL)
	{
		DeleteObject(bitmap_h_);
		bitmap_h_ = NULL;
	}
}

void UIImage::SetImage(HBITMAP bmp, const UIRect &image_src_rect, const UIRect &image_zoom_guardlines)
{
	preset_image_.image_name.clear();
	if(bitmap_h_ != NULL)
	{
		DeleteObject(bitmap_h_);
		bitmap_h_ = NULL;
	}
	bitmap_h_ = bmp;
	preset_image_.image_src_rect = image_src_rect;
	preset_image_.image_zoom_guardlines = image_zoom_guardlines;
}

bool UIImage::ApplyAttributes(IUIBuilderInterface *ui_builder, 
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	if(!__super::ApplyAttributes(ui_builder, xml_document, node))
		return false;

	UIXmlAttribute attribute = NULL;

	attribute = xml_document->FirstAttribute(node, "fore_image");
	if(attribute != NULL)
	{
		UIPresetImage img;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), img) == 1)
		{
			SetImage(img);
		}
	}

	return true;
}

void UIImage::OnRenderForeground(const UIRenderContext &rc)
{
	if(bitmap_h_ != NULL)
	{
		UIImageD img_data = rc.rr->GenerateImageFromHandle(bitmap_h_);
		UIPresetImageInfo img_info;
		if(rc.rr->CheckImageDInfo(img_data, img_info))
		{
			UIRect rt_src = preset_image_.image_src_rect;
			if(rt_src.Width() <= 0)
			{
				rt_src.left = 0;
				rt_src.right = img_info.cx;
			}
			if(rt_src.Height() <= 0)
			{
				rt_src.top = 0;
				rt_src.bottom = img_info.cy;
			}
			rc.rr->DrawImage(rc.dc, img_data, &rt_src,
				rc.client_pos_on_client, &preset_image_.image_zoom_guardlines);
		}
		rc.rr->DestroyImage(img_data);
	}else
	{
		UIPresetImageInfo img_info;
		if(!rc.rr->CheckPresetImageInfo(preset_image_, img_info))
			return;

		UIRect rt_src = preset_image_.image_src_rect;
		if(rt_src.Width() <= 0)
		{
			rt_src.left = 0;
			rt_src.right = img_info.cx;
		}
		if(rt_src.Height() <= 0)
		{
			rt_src.top = 0;
			rt_src.bottom = img_info.cy;
		}
		rc.rr->DrawImage(rc.dc, preset_image_.image_name, &rt_src,
			rc.client_pos_on_client, &preset_image_.image_zoom_guardlines);
	}
}

int UIImage::GetControlAutoWidth()
{
	int ret = __super::GetControlAutoWidth();
	if(ret > 0)
		return ret;

	if(bitmap_h_ != NULL)
	{
		BITMAP bm;
		if(!::GetObject(bitmap_h_, sizeof(bm), &bm))
			return 0;
		return bm.bmWidth;
	}

	return CheckPreseImageSize(preset_image_).cx;
}

int UIImage::GetControlAutoHeight()
{
	int ret = __super::GetControlAutoHeight();
	if(ret > 0)
		return ret;

	if(bitmap_h_ != NULL)
	{
		BITMAP bm;
		if(!::GetObject(bitmap_h_, sizeof(bm), &bm))
			return 0;
		return bm.bmHeight;
	}

	return CheckPreseImageSize(preset_image_).cy;
}

_SSUIL_END
_SSL_END
